<template>
  <div>
    <SearchContainer @search="getData(), getAmount()" @reset="() => reset()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="" class="ml-2">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="姓名/助记码/住院号/床位号" @keydown.enter="getData" />
        </a-form-item>
        <a-form-item name="dateRange" label="收费日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="onChangeRangePicker" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex justify-start">
        <a-button class="mb-2 mr-2" type="primary" @click="onDepositModel"> 收取预收款 </a-button>
        <a-button class="mb-2"> 导出 </a-button>
      </div>
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 1800 }" column-code="000088" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'no'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'zyStatusStr'">
            <!-- inpRecordStatus 0,'在院',1,'已出院',2,'待出院',3,'待入区','' -->
            <a-tag v-if="record.inpRecordStatus === '0'" color="blue">在院</a-tag>
            <a-tag v-else-if="record.inpRecordStatus === '1'" color="green">已出院</a-tag>
            <a-tag v-else-if="record.inpRecordStatus === '2'" color="orange">待出院</a-tag>
            <a-tag v-else-if="record.inpRecordStatus === '3'" color="purple">待入区</a-tag>
            <span v-else>-</span>
          </template>
          <template v-if="column.dataIndex === 'yjStatusStr'">
            <a-tag v-if="record.yjStatusStr === '未使用' || record.yjStatusStr === '使用中'" color="green">正常</a-tag>
            <a-tag v-if="record.yjStatusStr === '已使用' || record.yjStatusStr === '已退还'" color="blue">结清</a-tag>
          </template>
          <template v-if="column.dataIndex === 'jhzt'">
            <a-tag v-if="record.jhzt === 0" color="green">待确认</a-tag>
            <a-tag v-if="record.jhzt === 1" color="blue">已确认</a-tag>
            <a-tag v-if="record.jhzt === 2" color="red">已作废</a-tag>
          </template>
          <template v-if="column.dataIndex === 'djlb'">
            <a-tag v-if="record.djlb === '0'" color="green">药材</a-tag>
            <a-tag v-if="record.djlb === '1'" color="blue">物资</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <!-- record.status  0未使用1使用中2已使用3已退还 -->
            <a-button v-if="record.status === 0 || record.status === 1" type="link" size="small" @click="onPrint(record)">补打单据</a-button>
            <a-divider v-if="record.status === 0 || record.status === 1" type="vertical" />
            <a-button v-if="record.status === 0 || record.status === 1" type="link" danger size="small" @click="openReturnChargeModel(record)"
              >退还预收款</a-button
            >
            <span v-if="record.status === 2 || record.status === 3">-</span>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <template v-for="column in columnsData" :key="column.dataIndex">
                <a-table-summary-cell v-if="column.dataIndex === 'no'"> 合计 </a-table-summary-cell>
                <a-table-summary-cell v-else-if="!column.isHide">
                  <div :class="amountTatol[column.dataIndex] ? 'text-right' : 'text-center'">
                    {{ amountTatol[column.dataIndex] ? formatAmount(amountTatol[column.dataIndex]) : '-' }}
                  </div>
                </a-table-summary-cell>
              </template>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <DepositModel
      v-model:open="depositModelOpen"
      :patient-info="patientInfo"
      :deposit-type="1"
      :search-params="{ statuss: ['0', '3'] }"
      placeholder-info="姓名/助记码/住院号/床位号"
      @success="depositSuccess"
    />
    <!-- 退费弹窗 -->
    <ChargeModal
      v-model:open="returnChargeModel"
      :is-show-in-title="true"
      :charge-title="chargeTitle"
      :register-price="returnChargeInfo.returnJe"
      :deposit-type="1"
      @success="onReturnSuccessPrice"
    />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
// import { useRouter } from 'vue-router'
import { formatAmount } from '@/utils/format.js'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import ChargeModal from '@/components/ChargeModal/return'
import DepositModel from '@/views/in-registration/components/DepositModel.vue'

// const router = useRouter()
const rowData = ref(null)
const formRef = ref(null)
const patientInfo = ref({})
const returnChargeInfo = ref({})
const depositModelOpen = ref(false)
const returnChargeModel = ref(false)
const chargeTitle = ref('')
const formState = ref({
  stdate: dayjs(new Date().setDate(new Date().getDate() - 7)).format('YYYY-MM-DD'),
  eddate: dayjs().format('YYYY-MM-DD'),
  zjm: '',
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 7)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/inpComprehensive/getChargeList',
  immediate: true,
  params: formState.value
})
const amountTatol = ref({
  dpJe: 0
})
const getAmount = async () => {
  const res = await post('/inp/inpComprehensive/countChargeList', {
    stdate: formState.value.dateRange?.[0],
    eddate: formState.value.dateRange?.[1],
    zjm: formState.value.zjm
  })
  if (res.code === 0) {
    amountTatol.value.dpJe = res.data || 0
  }
}
//获取表头
const columnsData = ref([])
const getTableColumns = async () => {
  const { code, data } = await post('/common/util/getTableMappingCloumnList', { id: '000088' })
  if (code !== 0) return message.error(code)
  columnsData.value = data
}
const onPrint = async (row) => {
  const { code, msg, data } = await post('/inp/Print/inpDepositSlipPrint', { lsh: row?.lsh, inplsh: row?.inplsh, sfbd: 1 })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const openReturnChargeModel = async (record) => {
  returnChargeInfo.value = record
  returnChargeInfo.value.returnJe = record.dpJe
  const res = await post('/inp/inpComprehensive/getBillAmount', {
    inplsh: record.inplsh
  })
  if (res.code !== 0) {
    message.error(res.msg || '操作失败')
    return
  } else {
    chargeTitle.value = res.data.memo
    returnChargeModel.value = true
  }
}
const onDepositModel = () => {
  rowData.value = null
  depositModelOpen.value = true
}
const onReturnSuccessPrice = async (params) => {
  const res = await post('/inp/inpComprehensive/doReturnCharge', {
    typeOperation: '2',
    lsh: returnChargeInfo.value.lsh,
    ...params
  })
  if (res.code !== 0) {
    message.error(res.msg || '操作失败')
    return
  } else {
    message.success(res.msg)
    returnChargeModel.value = false
    getData()
  }
}
const depositSuccess = () => {
  getData()
}
const onChangeRangePicker = (e) => {
  formState.value.stdate = e[0]
  formState.value.eddate = e[1]
}
const reset = () => {
  formRef.value.resetFields()
  getData()
}
onMounted(() => {
  getAmount()
  getTableColumns()
})
</script>
